package com.knife.dom;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * create by yongli on 2021-11-18 10:55
 */

public class SapTransfer {

    @Test
    public void test() {

        RestTemplate restTemplate = new RestTemplate();
        String path = "/Users/yongli/Desktop/11-16-2.xlsx";

        JSONArray list = new JSONArray();
        try {
            File file = new File(path);
            Workbook workbook = WorkbookFactory.create(file);
            Sheet sheet = workbook.getSheetAt(0);
            int col = sheet.getRow(0).getPhysicalNumberOfCells();
            int num = sheet.getLastRowNum();
            // 从第2行开始读取
            for (int i = 2; i < num + 1; i++) {

                // 从第二行开始
                Row row = sheet.getRow(i);

                String val = row.getCell(2).getStringCellValue();
                if (val.contains("BUSINESS_TABLE")) {
                    val = val.replaceAll("BUSINESS_TABLE", "\"ID_FOR_TABLE");
                    val = val.replace("\"FIELD2\":\",", "");
                    val = val.substring(0, val.length() - 1);
                    JSONObject jsonObject = JSONObject.parseObject(val);
                    JSONArray arr = jsonObject.getJSONArray("ZDOMC");
                    list.addAll(arr);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(list.size());

        List<String> handle = new ArrayList<>();

        String pro = "https://s3.rootcloud.com/sany-heavy-energy-base/api/active-sap/transfers/sendParams";
        String test = "http://sany-heavy-energy-base.bdn-saas-iot-qa.rootcloudapp.com/api/active-sap/transfers/sendParams";
        List<String> cookies = new ArrayList<>();
        cookies.add("access_token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJhZG1pbl84azk2NjAxNiIsImF1dGhvcml0aWVzIjpbIlJPTEVfQURNSU4iLC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wiaXNPcGVyYXRvciI6dHJ1ZSwibmFtZSI6IuS4ieS4gOmHjeiDveaciemZkOWFrOWPuCIsIm1ldGFkYXRhIjp7Im1pZ3JhdGlvbiI6dHJ1ZSwibGVnYWN5Ijp7ImFkbWluX2lkIjoiTlM5SDU4MDEifX19LCJzY29wZSI6W10sImlzTG9nQXMiOmZhbHNlLCJhcHBJZCI6bnVsbCwibm9uY2UiOiIyM2JmOTlmYS04MDJmLTQzOTQtYWNhZC02ZTg3ODFlYjM4ZjEiLCJpYXQiOjE2MzcyMTgyMDQsImV4cCI6MTYzODQyNzgwNH0.hNrmIIPYHQt1ltC6XX_Ljzu7h1z3nzfYzjCfs71J_HzLSFBh0gZWCa4tw1UbEQm3JiwkgQSl17MRsIXfpd522nFZBtCPemEeN4yZsYyt5-Tgogp6Zy_qqBN8u13FAZ87xBUefkO0gKhsK9PvtD2fd7AgvM0J5VNLbSjaPIosups; Path=/; Domain=rootcloud.com;");
        try {
            File file = new File(path);
            Workbook workbook = WorkbookFactory.create(file);
            Sheet sheet = workbook.getSheetAt(0);
            int col = sheet.getRow(0).getPhysicalNumberOfCells();
            int num = sheet.getLastRowNum();
            // 从第2行开始读取

            for (int i = 6; i < num + 1; i++) {

                // 从第二行开始
                Row row = sheet.getRow(i);

                String val = row.getCell(2).getStringCellValue();
                if (val.contains("BUSINESS_TABLE")) {
                    val = val.replaceAll("BUSINESS_TABLE", "\"ID_FOR_TABLE");
                    val = val.replace("\"FIELD2\":\",", "");
                    val = val.substring(0, val.length() - 1);
                    JSONObject jsonObject = JSONObject.parseObject(val);
                    JSONArray arr = jsonObject.getJSONArray("ZDOMC");

                } else {
                    // zdomj 会出现两次
                    if (handle.contains(val)) {
                        continue;
                    }
                    handle.add(val);
                    val = val.replaceAll("ZDOMJ", "\"ZDOMJ");
                    val = val.substring(0, val.length() - 1);
                    JSONObject jsonObject = JSONObject.parseObject(val);
                    JSONArray arr = jsonObject.getJSONArray("ZDOMJ");
                    for (int j = 0; j < arr.size(); j++) {
                        JSONObject e = arr.getJSONObject(j);
                        if (e.getString("TYPE").equals("E") && !e.getString("MESSAGE").contains("RFC_POP锁定")) {
                            System.out.println(e.toJSONString());
                            String ID = e.getString("ID");
                            for (int k = 0; k < list.size(); k++) {
                                JSONObject ma = list.getJSONObject(k);
                                if (ma.getString("ID").equals(ID)) {
                                    System.out.println(ma.toJSONString());

                                    HttpHeaders headers = new HttpHeaders();
                                    headers.put(HttpHeaders.COOKIE, cookies);
                                    JSONObject param = new JSONObject();
                                    param.put("ID_FOR_TABLE", "MM_NEW_MATERIAL");
                                    JSONArray js = new JSONArray();
                                    js.add(ma);
                                    param.put("ZDOMC", js);
                                    System.out.println(param.toJSONString());
                                    // 10s
//                                    Thread.sleep(10000);
//                                    HttpEntity<Object> httpEntity = new HttpEntity<>(param, headers);
//                                    ResponseEntity<String> responseEntity = restTemplate.postForEntity(
//                                            pro, httpEntity, String.class);
//                                    System.out.println(responseEntity);
                                }
                            }

                        }
                    }
                }

            }
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    @Test
    public void main3() {

        RestTemplate restTemplate = new RestTemplate();
        String path = "/Users/yongli/Desktop/11-16.xlsx";
        List<QisWorkOrderBean> beanList = new ArrayList<>();
        try {
            File file = new File(path);
            Workbook workbook = WorkbookFactory.create(file);
            Sheet sheet = workbook.getSheetAt(0);
            int col = sheet.getRow(0).getPhysicalNumberOfCells();
            int num = sheet.getLastRowNum();
            // 从第2行开始读取
            num = 26;
            for (int i = 24; i < num + 1; i++) {

                // 从第二行开始
                Row row = sheet.getRow(i);

                String val = row.getCell(6).getStringCellValue();
                val = val.replaceAll("BUSINESS_TABLE", "\"ID_FOR_TABLE");
                val = val.replace("\"FIELD2\":\",", "");
                val = val.substring(0, val.length() - 1);

                JSONObject jsonObject = JSONObject.parseObject(val);
//
//                JSONObject js = JSONObject.parseObject(val);
//                System.out.println("json");
//                System.out.println(js.toJSONString());

                System.out.println(JSON.toJSONString(val));
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    @Test
    public void main2() {

        RestTemplate restTemplate = new RestTemplate();
        String path = "/Users/yongli/Desktop/11-16.xlsx";
        List<QisWorkOrderBean> beanList = new ArrayList<>();
        try {
            File file = new File(path);
            Workbook workbook = WorkbookFactory.create(file);
            Sheet sheet = workbook.getSheetAt(0);
            int col = sheet.getRow(0).getPhysicalNumberOfCells();
            int num = sheet.getLastRowNum();
            // 从第2行开始读取
            num = 26;
            for (int i = 24; i < num + 1; i++) {

                // 从第二行开始
                Row row = sheet.getRow(i);

                String val = row.getCell(6).getStringCellValue();
                val = val.replaceAll("BUSINESS_TABLE", "\"ID_FOR_TABLE");
                val = val.replace("\"FIELD2\":\",", "");
                val = val.substring(0, val.length() - 1);

//                System.out.println(val);
//                Thread.sleep(150);
                // cookie的形式 : val=key;
                List<String> arr = new ArrayList<>();
//                arr.add("access_token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.-e7n-euoeeQhuWRmCIsIm1ldGFkYXRhIjp7Im1pZ3JhdGlvbiI6dHJ1ZX19LCJ0ZW5hbnRJZCI6IjhLOTY2MDEiLCJhY3RpdmVDb21wYW55Ijp7ImlkIjoiOEs5NjYwMSIsImFkbWlucyI6WyJOUzlINTgwMSJdLCJzdGF0dXMiOiJSZWd1bGFyIiwiaXNPcGVyYXRvciI6dHJ1ZSwibmFtZSI6IuS4ieS4gOmHjeiDveaciemZkOWFrOWPuCIsIm1ldGFkYXRhIjp7Im1pZ3JhdGlvbiI6dHJ1ZSwibGVnYWN5Ijp7ImFkbWluX2lkIjoiTlM5SDU4MDEifX19LCJhY3RpdmVPcmdhbml6YXRpb24iOnsiaWQiOiI4Szk2NjAxIiwiYWRtaW5zIjpbIk5TOUg1ODAxIl0sInN0YXR1cyI6IlJlZ3VsYXIiLCJpc09wZXJhdG9yIjp0cnVlLCJuYW1lIjoi5LiJ5LiA6YeN6IO95pyJ6ZmQ5YWs5Y-4IiwibWV0YWRhdGEiOnsibWlncmF0aW9uIjp0c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yJpZCI6IjhLOTY2MDEiLCJhZG1pbnMiOlsiTlM5SDU4MDEiXSwic3RhdHVzIjoiUmVndWxhciIsImlzT3BlcmF0b3IiOnRydWUsIm5hbWUiOiLkuInkuIDph43og73mnInpmZDlhazlj7giLCJtZXRhZGF0YSI6eyJtaWdyYXRpb24iOnRydWUsImxlZ2FjeSI6eyJhZG1pbl9pZCI6Ik5TOUg1ODAxIn19fSwiYWN0aXZlT3JnYW5pemF0aW9uIjp7ImlkIjoiOEs5NjYwMSIsImFkbWlucyI6WyJOUzlINTgwMSJdLCJzdGF0dXMiOiJSZWd1bGFyIiwiaXNPcGVyYXRvciI6dHJ1ZSwibmFtZSI6IuS4ieS4gOmHjeiDveaciemZkOWFrOWPuCIsIm1ldGFkYXRhIjp7Im1pZ3JhdGlvbiI6dHJ1ZSwibGVnYWN5Ijp7ImFkbWluX2lkIjoiTlM5SDU4MDEifX19LCJzY29wZSI6W10sImlzTG9nQXMiOmZhbHNlLCJhcHBJZCI6bnVsbCwibm9uY2UiOiI4NmU4ODNmYS1kYzg1LTRjNjctYjJkZC1lMTI4NmZmYmU1ZmEiLCJpYXQiOjE2MzcyMDY5NTEsImV4cCI6MTYzODQxNjU1MX0.T4vSQ2_lY962S7U9PoXHwfpqcva8dhFRmUbuujMyNmQChaPCH8jjVQULOk7AISoD8xSVf7ABh4AYwDB4kSJrj9ioUCBWNgZ68i-KBU9jalRcHjP6loa6fI_AkbkUUTX4IyYSyeaIJXvOfPeWnU11gwJz08oW6gzcglnVnLIKF-0; Path=/; Domain=rootcloud.com;");
//                System.out.println(val);
                HttpHeaders headers = new HttpHeaders();
                // 手动置入cookie
                headers.put(HttpHeaders.COOKIE, arr);
                JSONObject jsonObject = JSONObject.parseObject(val);

//                MultiValueMap params = new LinkedMultiValueMap(JSON.toJSON(val));
//                params.putAll(JSONObject.toJSON.toJSON(val));
                JSONObject js = JSONObject.parseObject(val);
                System.out.println("json");
                System.out.println(js.toJSONString());
                // json存成字符串时必需要转义。但是json在传输啊,显示啊都可以不传。
                System.out.println(JSON.toJSONString(val));

                String test = "http://sany-heavy-energy-base.bdn-saas-iot-qa.rootcloudapp.com/api/active-sap/transfers/sendParams";
                String pro = "https://s3.rootcloud.com/sany-heavy-energy-base/api/active-sap/transfers/sendParams";

//                HttpEntity<Object> httpEntity = new HttpEntity<>(headers);
//                ResponseEntity<String> responseEntity  =restTemplate.postForEntity(
//                        "https://s3.rootcloud.com/auth/verify-token",httpEntity,String.class);
//                System.out.println(responseEntity.getBody());
                // 800ms，不足1s,间隔有点小，可以大点
//                Thread.sleep(2000);
//                HttpEntity<Object> httpEntity = new HttpEntity<>(jsonObject, headers);
//                ResponseEntity<String> responseEntity = restTemplate.postForEntity(
//                        pro, httpEntity, String.class);
//                System.out.println(responseEntity);
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
    }
}
